import { Controller, Post, Body, Inject, Logger } from '@midwayjs/core';
import { UserService } from '../service/user.service';
import { LoginDTO, RegisterDTO } from '../dto/user.dto';
import { ILogger } from '@midwayjs/logger';

@Controller('/api')
export class AuthController {
  @Inject()
  userService: UserService;

  @Logger()
  logger: ILogger;

  /**
   * 用户登录接口
   */
  @Post('/login')
  async login(@Body() loginDTO: LoginDTO) {
    try {
      this.logger.info('[AuthController] 处理登录请求');
      const result = await this.userService.login(loginDTO);
      this.logger.info('[AuthController] 登录请求处理完成');
      return {
        code: 200,
        message: '登录成功',
        data: result
      };
    } catch (error) {
      this.logger.error('[AuthController] 登录请求处理失败', error);
      return {
        code: 400,
        message: error.message || '登录失败',
        data: null
      };
    }
  }

  /**
   * 用户注册接口
   */
  @Post('/register')
  async register(@Body() registerDTO: RegisterDTO) {
    try {
      this.logger.info('[AuthController] 处理注册请求');
      const result = await this.userService.register(registerDTO);
      this.logger.info('[AuthController] 注册请求处理完成');
      return {
        code: 200,
        message: '注册成功',
        data: result
      };
    } catch (error) {
      this.logger.error('[AuthController] 注册请求处理失败', error);
      return {
        code: 400,
        message: error.message || '注册失败',
        data: null
      };
    }
  }
}